package event

import (
	"context"
	"github.com/gin-gonic/gin"
	"inspector/internal/model/do"
	"inspector/pkg/result"
	"inspector/pkg/validation"
)

func (a *Controller) Webhook(c *gin.Context) {

	var rb do.WebhookRequest
	if err := validation.ValidateAll(c, &rb); err != nil {
		c.Error(err)
		return
	}
	eventType := c.GetHeader("X-Atomgit-Event")
	hookId := c.GetHeader("X-Atomgit-Hook-Id")
	event := do.WebhookEvent{
		EventType:      eventType,
		HookId:         hookId,
		WebhookPayload: rb,
	}
	err := a.srv.ProcessWebhookEvent(context.TODO(), event)
	if err != nil {
		c.Error(err)
		return
	}

	result.SendResponse(c, "success", nil)
}
